package gk.example.activity;

import gk.example.BaseActivity;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.gkdate.R;

public class GetHeadActivity extends BaseActivity implements OnClickListener {
	private Button mBtnAlbum;
	private Button mBtnCamera;
	private ImageButton mBtnBack;
	private ImageView mImageView;
	private TextView mTvTitle;
	private static final String MPHOTOPATH = Environment
			.getExternalStorageDirectory() + "/GkDate/";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	}

	@Override
	protected void initView() {
		setContentView(R.layout.activity_get_head);
		mTvTitle = (TextView) findViewById(R.id.tv_top_title);
		mBtnAlbum = (Button) findViewById(R.id.btn_head_album);
		mBtnCamera = (Button) findViewById(R.id.btn_head_camera);
		mImageView = (ImageView) findViewById(R.id.iv_head_pre_setting);
		mBtnBack = (ImageButton) findViewById(R.id.btn_back);
	}

	@Override
	protected void initEvent() {
		mBtnAlbum.setOnClickListener(this);
		mBtnCamera.setOnClickListener(this);
		mBtnBack.setOnClickListener(this);
		mTvTitle.setText(getResources().getString(R.string.head_setting));
	}

	/** 拍照 **/
	private static final int PHOTO_REQUEST_CAMERA = 1;
	/** 相册 **/
	private static final int PHOTO_REQUEST_ALBUM = 2;

	/** 裁剪结果 **/
	private static final int PHOTO_REQUEST_CUT = 3;
	private File mPhotoFile;

	/**
	 * 
	 * 描述：从相册中选择头像
	 * 
	 * @return void
	 */
	private void getHeadFromAlbum() {
		Intent intent = new Intent(Intent.ACTION_PICK, null);
		intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
				"image/*");
		startActivityForResult(intent, PHOTO_REQUEST_ALBUM);
	}

	/**
	 * 
	 * 描述： 通过拍照获取头像
	 * 
	 * @return void
	 */
	private void getHeadFromCamera() {
		mPhotoFile = new File(MPHOTOPATH, getPhotoFileName());
		Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
		// 指定调用相机拍照后照片的储存路径
		intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mPhotoFile));
		startActivityForResult(intent, PHOTO_REQUEST_CAMERA);
	}

	/**
	 * 
	 * 描述：使用日期来为照片命名
	 * 
	 * @return 返回照片文件名
	 * @return String
	 */
	private String getPhotoFileName() {
		Date date = new Date(System.currentTimeMillis());
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"'IMG'_yyyyMMdd_HHmmss");
		return dateFormat.format(date) + ".JPG";
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		switch (requestCode) {
		case PHOTO_REQUEST_CAMERA:
			startPhotoZoom(Uri.fromFile(mPhotoFile), 250);
			break;
		case PHOTO_REQUEST_ALBUM:
			if (data != null)
				startPhotoZoom(data.getData(), 250);
			break;
		case PHOTO_REQUEST_CUT:
			if (data != null)
				setPicToView(data);
			break;

		default:
			break;
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

	/**
	 * 
	 * 描述：利用安卓自带的裁剪功能对图片进行裁剪
	 * 
	 * @param uri
	 *            图片地址
	 * @param size
	 *            图片大小
	 * @return void
	 */
	private void startPhotoZoom(Uri uri, int size) {
		Intent intent = new Intent("com.android.camera.action.CROP");
		intent.setDataAndType(uri, "image/*");
		// crop为true是设置在开启的intent中设置显示的view可以剪裁
		intent.putExtra("crop", "true");

		// aspectX aspectY 是宽高的比例
		intent.putExtra("aspectX", 1);
		intent.putExtra("aspectY", 1);

		// outputX,outputY 是剪裁图片的宽高
		intent.putExtra("outputX", size);
		intent.putExtra("outputY", size);
		intent.putExtra("return-data", true);

		startActivityForResult(intent, PHOTO_REQUEST_CUT);
	}

	/**
	 * 
	 * 描述：将图片设置到View上
	 * 
	 * @param picdata
	 * @return void
	 */
	private void setPicToView(Intent picdata) {
		Bundle bundle = picdata.getExtras();
		if (bundle != null) {
			Bitmap photo = bundle.getParcelable("data");
			mImageView.setImageBitmap(photo);
		}
	}

	@Override
	public void onClick(View v) {

		File mCreateDir = new File(MPHOTOPATH);
		if (!mCreateDir.exists()) {
			mCreateDir.mkdirs();
		}
		switch (v.getId()) {

		case R.id.btn_head_album:
			getHeadFromAlbum();
			break;
		case R.id.btn_head_camera:
			getHeadFromCamera();
			break;
		default:
			break;
		}
	}

}
